package com.sqfw.project.wp.service;

import com.sqfw.common.exception.base.BaseException;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.wp.domain.SysMiniFile;
import com.sqfw.project.wp.domain.WpHandleLog;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.domain.vo.WpDetailVO;
import com.sqfw.project.wp.mapper.SysMiniFileMapper;
import com.sqfw.project.wp.mapper.WpHandleLogMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QualityAuditService {

    @Autowired
    private WpInfoMapper wpInfoMapper;
    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;
    @Autowired
    private SysMiniFileMapper sysMiniFileMapper;

    /**
     * 质检查看详情
     */
    public WpDetailVO showDetail(String scid){
        if(StringUtils.isEmpty(scid)){
            throw new BaseException("工单主键为空");
        }

        WpInfo wpInfo = wpInfoMapper.selectWpInfoByScid(scid);
        if(null == wpInfo){
            throw new BaseException("工单不存在");
        }
        WpDetailVO.CommUserInfo commUserInfo = WpDetailVO.CommUserInfo.builder()
                .name(wpInfo.getCustomerName())
                .age(wpInfo.getAge())
                .genner(wpInfo.getGender())
                .idno(wpInfo.getCardnum())
                .phone1(wpInfo.getPhoneno())
                .phone2(wpInfo.getMobile())
                .addrLeave1(wpInfo.getDistrict())
                .addrLeave2(wpInfo.getStreet())
                .addrLeave3(wpInfo.getCommittee())
                .addrLeave4(wpInfo.getRelAddress())
                .build();

        // 查询工单轨迹列表
        WpHandleLog wpHandleLogQuery = new WpHandleLog();
        wpHandleLogQuery.setProcessId(scid);
        List<WpHandleLog> wpHandleLogList = wpHandleLogMapper.selectWpHandleLogList(wpHandleLogQuery);

        // 查询文件列表
        SysMiniFile sysMiniFileQuery = new SysMiniFile();
        sysMiniFileQuery.setWpScid(scid);
        List<SysMiniFile> miniFileList = sysMiniFileMapper.selectSysMiniFileList(sysMiniFileQuery);

        WpDetailVO wpDetailVO = WpDetailVO.builder()
                .commUserInfo(commUserInfo)
                .wpHandleLogList(wpHandleLogList)
                .wpInfo(wpInfo)
                .sysMiniFileList(miniFileList)
                .build();
        return wpDetailVO;
    }

    /**
     * 质检审核提交
     * @return
     */
    public String submit(WpInfo wpInfo) {

        if(StringUtils.isEmpty(wpInfo.getScid())){
            throw new BaseException("工单主键为空");
        }

        WpInfo gongdan = wpInfoMapper.selectWpInfoByScid(wpInfo.getScid());
        if(null == gongdan){
            throw new BaseException("工单不存在");
        }
        if (StringUtils.isEmpty(wpInfo.getClass1())){
            wpInfo.setClass1(gongdan.getClass1());
            wpInfo.setClass2(gongdan.getClass2());
            wpInfo.setClass3(gongdan.getClass3());
            wpInfo.setClass4(gongdan.getClass4());
            wpInfo.setFiveMajorModules(gongdan.getFiveMajorModules());
        }
        // 更新工单相关状态
        wpInfoMapper.updateWpInfo(wpInfo);

        WpHandleLog wpHandleLog = new WpHandleLog();

        String processId = wpHandleLogMapper.getWpHandleLogId();
        wpHandleLog.setProcessId("SQ" + processId);
        int i = wpHandleLogMapper.insertWpHandleLog(wpHandleLog);
        return null;
    }

    public List<WpInfo> getList(WpInfo wpInfo) {
        List<WpInfo> wpInfoList = wpInfoMapper.selectWpInfoList(wpInfo);
        return wpInfoList;
    }
}
